package com.vaadin.tutorial.crm.ui.view.dashboard;

import com.vaadin.flow.component.Tag;
import com.vaadin.flow.component.charts.Chart;
import com.vaadin.flow.component.charts.model.*;
import com.vaadin.flow.component.dependency.CssImport;
import com.vaadin.flow.component.dependency.JsModule;
import com.vaadin.flow.component.html.H2;
import com.vaadin.flow.component.littemplate.LitTemplate;
import com.vaadin.flow.component.template.Id;
import com.vaadin.flow.router.AfterNavigationEvent;
import com.vaadin.flow.router.AfterNavigationObserver;
import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route;
import com.vaadin.tutorial.crm.backend.entity.Score;
import com.vaadin.tutorial.crm.backend.entity.Student;
import com.vaadin.tutorial.crm.backend.service.CourseService;
import com.vaadin.tutorial.crm.backend.service.ScoreService;
import com.vaadin.tutorial.crm.backend.service.StudentService;
import com.vaadin.tutorial.crm.backend.service.TeacherService;
import com.vaadin.tutorial.crm.ui.MainLayout;
import java.util.List;

@Route(value = "", layout = MainLayout.class)
@PageTitle("统计信息")
@JsModule("./src/views/dashboard/dashboard-view.ts")
@Tag("dashboard-view")
public class DashboardView extends LitTemplate implements AfterNavigationObserver {

    private StudentService studentService;
    private TeacherService teacherService;
    private CourseService courseService;
    private ScoreService scoreService;

    private List<Student> studentList;
    private List<String> stuNameList;
    private List<Score> scoreList;
    private String[] stuNames;
    private String[] courseNames;
    private List<Number> scoMathList;
    private List<Number> scoPhysicsList;
    private List<Number> scoEnglishList;
    private List<Number> scoComputerList;
    private List<Number> scoAlgebraList;
    private List<Number> scoCircuitList;
    private List<List<Number>> allScoList;
    private List<Number> totalScoList;

    @Id
    private Chart stuScore;
    
    @Id
    private Chart stuTotalScore;

    @Id
    private H2 totalStu;
    @Id
    private H2 totalTeacher;
    @Id
    private H2 totalCourse;

    //注意：vaadin所有变量都需要传入构造中作为参数，且这些变量的赋值操作都需要在构造中进行
    public DashboardView(StudentService studentService, TeacherService teacherService, CourseService courseService,
                         ScoreService scoreService, List<String> stuNameList,
                         List<Number> scoMathList, List<Number> scoPhysicsList,
                         List<Number> scoEnglishList, List<Number> scoComputerList,
                         List<Number> scoAlgebraList, List<Number> scoCircuitList,
                         List<List<Number>> allScoList, List<Number> totalScoList) {
        this.studentService = studentService;
        this.teacherService = teacherService;
        this.courseService = courseService;
        this.scoreService = scoreService;

        studentList = studentService.findAll();
        scoreList = scoreService.findAll();
        this.stuNameList = stuNameList;
        this.scoMathList = scoMathList;
        this.scoPhysicsList = scoPhysicsList;
        this.scoEnglishList = scoEnglishList;
        this.scoComputerList = scoComputerList;
        this.scoAlgebraList = scoAlgebraList;
        this.scoCircuitList = scoCircuitList;
        this.allScoList = allScoList;
        this.totalScoList = totalScoList;
        this.courseNames = new String[]{"高等数学", "大学物理", "大学英语", "计算机基础", "线性代数", "电路"};

        for(Score score : scoreList){
            Student stu = studentService.getStuListByStuId(score.getStudent().getStuId());
            this.stuNameList.add(stu.getStuName());
            this.scoMathList.add(score.getScoMath());
            this.scoPhysicsList.add(score.getScoPhysics());
            this.scoEnglishList.add(score.getScoEnglish());
            this.scoComputerList.add(score.getScoComputer());
            this.scoAlgebraList.add(score.getScoAlgebra());
            this.scoCircuitList.add(score.getScoCircuit());
            this.totalScoList.add(score.getScoMath() + score.getScoPhysics()
            + score.getScoEnglish() + score.getScoComputer()
            + score.getScoAlgebra() + score.getScoCircuit());
        }
        this.allScoList.add(scoMathList);
        this.allScoList.add(scoPhysicsList);
        this.allScoList.add(scoEnglishList);
        this.allScoList.add(scoComputerList);
        this.allScoList.add(scoAlgebraList);
        this.allScoList.add(scoCircuitList);
        this.stuNames = this.stuNameList.toArray(new String[this.stuNameList.size()]);
    }

    //页面加载时渲染统计图
    @Override
    public void afterNavigation(AfterNavigationEvent event) {
        boardView();
        stuScoreChart();
        stuTotalScoreChart();
    }

    //普通面板
    public void boardView(){
        totalStu.setText(String.valueOf(studentService.count()));
        totalTeacher.setText(String.valueOf(teacherService.count()));
        totalCourse.setText(String.valueOf(courseService.count()));
    }

    //直方图，展示每个学生各科成绩
    public void stuScoreChart(){
        Configuration configuration = stuScore.getConfiguration();
        for(int i = 0; i < this.courseNames.length; i++){
            configuration.addSeries(new ListSeries(courseNames[i], this.allScoList.get(i)));
        }

        XAxis x = new XAxis();
        x.setCrosshair(new Crosshair());
        x.setCategories(stuNames);
        configuration.addxAxis(x);
        YAxis y = new YAxis();
        y.setMin(0);
        configuration.addyAxis(y);
    }

    //学生个人总成绩折线图
    public void stuTotalScoreChart(){
        Configuration configuration = stuTotalScore.getConfiguration();
        configuration.addSeries(new ListSeries(totalScoList));
        XAxis x = new XAxis();
        x.setCrosshair(new Crosshair());
        x.setCategories(stuNames);
        configuration.addxAxis(x);
        YAxis y = new YAxis();
        y.setMin(0);
        configuration.addyAxis(y);
    }
}
